package idiet.view;

import idiet.Activator;
import idiet.bean.FoodMaterial;
import idiet.bean.Meal;
import idiet.service.MealService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class ViewPartMeal extends ViewPart {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("idiet.view.messages"); //$NON-NLS-1$
	public static final String ID = "iDiet.view.meal";
	private Logger logger=Logger.getLogger(ViewPartMeal.class);
	private Text textMealName;
	private Table table;
	private Table tableMealRecipe;
	private Table tableMealFeature;
	private List<Meal> meals=new ArrayList<Meal>();
	private MealService mealService=Activator.getApplicationContext().getBean(MealService.class);
	private TableViewer tableViewer;
	private TableViewer tableViewerMealFeature;
	private TableViewer tableViewerMealRecipe;
	public ViewPartMeal() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(3, false));
		
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(new BorderLayout(0, 0));
		GridData gd_group = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 2);
		gd_group.widthHint = 413;
		group.setLayoutData(gd_group);
		
		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayoutData(BorderLayout.NORTH);
		composite.setLayout(new GridLayout(3, false));
		
		Label lblPleaseKeyIn = new Label(composite, SWT.NONE);
		lblPleaseKeyIn.setText(BUNDLE.getString("ViewPartMeal.lblPleaseKeyIn.text")); //$NON-NLS-1$
		
		textMealName = new Text(composite, SWT.BORDER);
		GridData gd_textMealName = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textMealName.widthHint = 106;
		textMealName.setLayoutData(gd_textMealName);
		
		Button btnNewButtonQuery = new Button(composite, SWT.NONE);
		btnNewButtonQuery.setText(BUNDLE.getString("ViewPartMeal.btnNewButton.text")); //$NON-NLS-1$
		btnNewButtonQuery.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				meals=mealService.getMealsByMealName(textMealName.getText());
				tableViewer.setInput(meals);
			}
			
		});
		Group group_1 = new Group(group, SWT.NONE);
		group_1.setLayoutData(BorderLayout.CENTER);
		group_1.setLayout(new BorderLayout(0, 0));
		
		tableViewer = new TableViewer(group_1, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(BorderLayout.CENTER);
		tableViewer.setContentProvider(new ArrayContentProvider());
		table.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				IStructuredSelection selection =(IStructuredSelection)tableViewer.getSelection();
				for(Iterator itr=selection.iterator();itr.hasNext();){
					Meal meal=(Meal)itr.next();
					//foodIngredients=material.getFoodIngredients();
					//tableViewer.setInput(foodIngredients);
				}
			}
			
		});
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn.text")); //$NON-NLS-1$
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_2.text")); //$NON-NLS-1$
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_1.text")); //$NON-NLS-1$
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_6.setWidth(100);
		tblclmnNewColumn_6.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_6.text")); //$NON-NLS-1$
		
		Group grpMealsFeature = new Group(parent, SWT.NONE);
		grpMealsFeature.setText(BUNDLE.getString("ViewPartMeal.grpMealsFeature.text")); //$NON-NLS-1$
		grpMealsFeature.setLayout(new BorderLayout(0, 0));
		GridData gd_grpMealsFeature = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_grpMealsFeature.heightHint = 181;
		grpMealsFeature.setLayoutData(gd_grpMealsFeature);
		
		tableViewerMealFeature = new TableViewer(grpMealsFeature, SWT.BORDER | SWT.FULL_SELECTION);
		tableMealFeature = tableViewerMealFeature.getTable();
		tableMealFeature.setHeaderVisible(true);
		tableMealFeature.setLinesVisible(true);
		tableMealFeature.setLayoutData(BorderLayout.CENTER);
		
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(tableViewerMealFeature, SWT.NONE);
		TableColumn tblclmnNewColumn_7 = tableViewerColumn_7.getColumn();
		tblclmnNewColumn_7.setWidth(120);
		tblclmnNewColumn_7.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_7.text")); //$NON-NLS-1$
		
		TableViewerColumn tableViewerColumn_8 = new TableViewerColumn(tableViewerMealFeature, SWT.NONE);
		TableColumn tblclmnNewColumn_8 = tableViewerColumn_8.getColumn();
		tblclmnNewColumn_8.setWidth(800);
		tblclmnNewColumn_8.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_8.text")); //$NON-NLS-1$
		
		Group grpMealsRecipe = new Group(parent, SWT.NONE);
		grpMealsRecipe.setText(BUNDLE.getString("ViewPartMeal.grpMealsRecipe.text")); //$NON-NLS-1$
		grpMealsRecipe.setLayout(new BorderLayout(0, 0));
		GridData gd_grpMealsRecipe = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grpMealsRecipe.widthHint = 34;
		grpMealsRecipe.setLayoutData(gd_grpMealsRecipe);
		
		tableViewerMealRecipe = new TableViewer(grpMealsRecipe, SWT.BORDER | SWT.FULL_SELECTION);
		tableMealRecipe = tableViewerMealRecipe.getTable();
		tableMealRecipe.setHeaderVisible(true);
		tableMealRecipe.setLinesVisible(true);
		tableMealRecipe.setLayoutData(BorderLayout.CENTER);
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewerMealRecipe, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_3.text")); //$NON-NLS-1$
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewerMealRecipe, SWT.NONE);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_5.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_5.text")); //$NON-NLS-1$
		tblclmnNewColumn_5.setWidth(100);
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewerMealRecipe, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText(BUNDLE.getString("ViewPartMeal.tblclmnNewColumn_4.text")); //$NON-NLS-1$
		
		Group grpMealsPicture = new Group(parent, SWT.NONE);
		grpMealsPicture.setText(BUNDLE.getString("ViewPartMeal.grpMealsPicture.text")); //$NON-NLS-1$
		GridData gd_grpMealsPicture = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grpMealsPicture.widthHint = 183;
		grpMealsPicture.setLayoutData(gd_grpMealsPicture);
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
